package com.Khan.SocialRescueSys.controller.admin;

import com.Khan.SocialRescueSys.pojo.Message;
import com.Khan.SocialRescueSys.pojo.User;
import com.Khan.SocialRescueSys.service.MessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Khan Liu
 * @date 2022/4/6 10:48
 */
@Controller
@RequestMapping("/admin")
public class AdminMessageController {

    @Autowired
    private MessageService messageService;

    @GetMapping("/message")
    public String message(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum, Model model){
        PageHelper.startPage(pagenum, 5);
        List<Message> allMessage = messageService.getAllMessages();
        //得到分页结果对象
        PageInfo<Message> pageInfo = new PageInfo<>(allMessage);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/message";
    }

    @GetMapping("/message/input")
    public String toAddMessage(Model model){
        model.addAttribute("message", new Message());   //返回一个message对象给前端th:object
        return "admin/message-input";
    }

    @GetMapping("/message/{id}/input")
    public String toEditMessage(@PathVariable Long id, Model model){
        model.addAttribute("message", messageService.getMessage(id));
        return "admin/message-input";
    }

    @PostMapping("/message")
    public String addMessage(Message message, HttpSession session, RedirectAttributes attributes){   //新增
        message.setUser((User) session.getAttribute("user"));
        message.setUserId(message.getUser().getId());
        message.setUpdateTime(new Date());
        message.setContent(markdownToHtml(message.getContent()));
        if(message.getFirstPicture()==null || message.getFirstPicture().equals("")){
            message.setFirstPicture("https://picsum.photos/seed/picsum/800/450");
        }
        if(message.getId() == null){
            message.setCreateTime(new Date());
            messageService.saveMessage(message);
            attributes.addFlashAttribute("msg", "新增成功");
        }else{
            messageService.updateMessage(message);
            attributes.addFlashAttribute("msg", "更新成功");
        }
        return "redirect:/admin/message";
    }

    public static String markdownToHtml(String markdown) {
        Parser parser = Parser.builder().build();
        Node document = parser.parse(markdown);
        HtmlRenderer renderer = HtmlRenderer.builder().build();
        return renderer.render(document);
    }


    @GetMapping("/message/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes){
        messageService.deleteMessage(id);
        attributes.addFlashAttribute("msg", "删除成功");
        return "redirect:/admin/message";
    }
}
